﻿@page
@model OpenPms.Areas.D.Pages.Setup.Rate.EditPriceModel
@{
    var weeks = "日一二三四五六";
    // var minDate = DateTime.Today.ToString("yyyy-MM-dd");
    // var today = DateOnly.FromDateTime(DateTime.Today);
    var rowIndex = 1;
    var isDelay = Model.IsDelayPrice;
    ViewBag.Title = Model.RateType + "-" + Model.RateName + "-" + (isDelay ? "超时房价" : "房价");
}
<nav aria-label='breadcrumb'>
    <ol class='breadcrumb'>

        <li class='breadcrumb-item'><a asp-page="./Index">房价码</a></li>
        <li class='breadcrumb-item active' aria-current='page'>@ViewBag.Title</li>
    </ol>
</nav>

<style>
    .form-control {
        width: 100px;
    }
</style>



<div class="row">
    <div class="col-md-4">
        <form id="FormGet">
            <table class="table table-striped">
                <thead>
                    <tr><th>#</th><th>有效期</th><th>天数</th><th></th></tr>
                </thead>
                <tbody>
                    @foreach (var item in Model.DisplayItems)
                    {
                        <tr>
                            <td>@(rowIndex++)</td>
                            <td>@item.Date.ToString("yyyy/MM/dd ddd")</td>
                            <td>@item.Days</td>
                            <td>
                                <a asp-page="./EditPrice" asp-route-date="@item.Date" asp-page-handler="">查看</a> |
                                <a href="javascript:;" onclick="Delete('@item.Date')">删除</a>
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
        </form>
    </div>
    <div class="col-md-8">
        @if (ViewBag.SaveResult != null)
        {
            <div class="alert alert-warning alert-dismissible fade show" role="alert" name="DivResult">
                @ViewBag.SaveResult
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        }

        <form method="post" id="FormPost">
            <div class="input-group">
                <span class="input-group-text" data-bs-title="包含当天" data-bs-toggle="tooltip">有效期</span>
                @if (isDelay)
                {
                    <input type="date" readonly="readonly" asp-for="Item.Date" asp-format="{0:yyyy-MM-dd}" class="form-control">
                }
                else
                {
                    <input type="date" asp-for="Item.Date" asp-format="{0:yyyy-MM-dd}" class="form-control">
                }
                <button class="btn btn-primary" type="submit" id="BtnSave">保存</button>
            </div>
            <table class="table  table-striped align-middle">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>房型</th>
                        <th>价格 <a href="javascript:;" onclick="CheckWeek(-1)">反选</a></th>
                        @for (int i = 0; i < weeks.Length; i++)
                        {
                            var id = "CB" + i;
                            <th name="@("TD"+i)">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" checked="checked" id="@id" name="CBWeek" onchange="CheckWeek(@i)">
                                    <label class="form-check-label" value="@i" for="@id">@weeks[i]</label>
                                </div>
                            </th>
                        }
                    </tr>
                </thead>
                <tbody class="table-group-divider">
                    @for (int i = 0; i < Model.EditItems.Count; i++)
                    {
                        <tr>
                            <th id="@("TH"+Model.EditItems[i].RoomTypeId)" rowindex="@i">
                                @(i + 1)
                                <input type="hidden" asp-for="EditItems[i].RoomTypeId" />
                                <input type="hidden" asp-for="EditItems[i].RoomTypeName" />
                                <input type="hidden" asp-for="EditItems[i].P0" />
                                <input type="hidden" asp-for="EditItems[i].P1" />
                                <input type="hidden" asp-for="EditItems[i].P2" />
                                <input type="hidden" asp-for="EditItems[i].P3" />
                                <input type="hidden" asp-for="EditItems[i].P4" />
                                <input type="hidden" asp-for="EditItems[i].P5" />
                                <input type="hidden" asp-for="EditItems[i].P6" />
                                <input type="hidden" asp-for="EditItems[i].D0" />
                                <input type="hidden" asp-for="EditItems[i].D1" />
                                <input type="hidden" asp-for="EditItems[i].D2" />
                                <input type="hidden" asp-for="EditItems[i].D3" />
                                <input type="hidden" asp-for="EditItems[i].D4" />
                                <input type="hidden" asp-for="EditItems[i].D5" />
                                <input type="hidden" asp-for="EditItems[i].D6" />
                            </th>
                            <th>@Model.EditItems[i].RoomTypeName</th>
                            <td><input name="TBAll" id="@("TBAll"+i)" type="number" class="form-control" asp-for="EditItems[i].All" onchange="SetPrice(@i)" /> </td>
                            @if (isDelay)
                            {
                                <td id="@("TD0"+i)" name="TD0">@Model.EditItems[i].D0</td>
                                <td id="@("TD1"+i)" name="TD1">@Model.EditItems[i].D1</td>
                                <td id="@("TD2"+i)" name="TD2">@Model.EditItems[i].D2</td>
                                <td id="@("TD3"+i)" name="TD3">@Model.EditItems[i].D3</td>
                                <td id="@("TD4"+i)" name="TD4">@Model.EditItems[i].D4</td>
                                <td id="@("TD5"+i)" name="TD5">@Model.EditItems[i].D5</td>
                                <td id="@("TD6"+i)" name="TD6">@Model.EditItems[i].D6</td>
                            }
                            else
                            {
                                <td id="@("TD0"+i)" name="TD0">@Model.EditItems[i].P0</td>
                                <td id="@("TD1"+i)" name="TD1">@Model.EditItems[i].P1</td>
                                <td id="@("TD2"+i)" name="TD2">@Model.EditItems[i].P2</td>
                                <td id="@("TD3"+i)" name="TD3">@Model.EditItems[i].P3</td>
                                <td id="@("TD4"+i)" name="TD4">@Model.EditItems[i].P4</td>
                                <td id="@("TD5"+i)" name="TD5">@Model.EditItems[i].P5</td>
                                <td id="@("TD6"+i)" name="TD6">@Model.EditItems[i].P6</td>
                            }
                        </tr>
                    }
                </tbody>
            </table>

        </form>

    </div>
</div>


@section Scripts {
    @{
        await Html.RenderPartialAsync("_ValidationScriptsPartial");
    }
    <script>
        var isDelay = "@(isDelay ? 1 : 0)";
        var changed = false;
        function CheckWeek(id) {
            if (id == -1) {
                for (var v = 0; v < 7; v++) {
                    $("#CB" + v).prop("checked", !$("#CB" + v).prop("checked"));
                }
            }
            if ($("[name='CBWeek']:checked").length)
                $("[name='TBAll']").removeAttr("disabled");
            else
                $("[name='TBAll']").attr("disabled", "");
        }
        function SetPrice(rowNo) {
            $("[name='DivResult'").remove();
            var value = $(`#TBAll${rowNo}`).val();
            for (var colNo = 0; colNo < 7; colNo++) {
                if ($(`#CB${colNo}`).prop("checked")) {
                    $(`#TD${colNo}${rowNo}`).html(value);
                    if (isDelay == 0)
                        $(`#EditItems_${rowNo}__P${colNo}`).val(value * 1);
                    else
                        $(`#EditItems_${rowNo}__D${colNo}`).val(value * 1);
                }
            }
        }
        function Delete(date) {
            ShowConfirm("您确定要删除吗？").on(function (yn) {
                if (yn) {
                    $("#FormPost").attr("action", '@Url.Page("./EditPrice")' + "?handler=delete&date=" + date);
                    $("#FormPost").submit();
                }
            });

        }
    </script>
}
